﻿using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XJLSC.PM
{
    public partial class JHRWGL : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        public JHRWGL()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String strSql = "";
            strSql = "SELECT D.ITEM_BUSINESS_ID AS ITEM_ID,MAX(XQRQ) 需求日期,MB036 前置天数,CONVERT(CHAR(8),DATEADD(DAY,MB036*(-1),MAX(XQRQ)),112) 预计开工,D.ITEM_CODE 品号,D.ITEM_NAME 品名,D.ITEM_SPECIFICATION 规格,CAST(SUM(Z.XQL) AS REAL) 需求数量,ISNULL(H.WORK_CENTER_NAME,'') 工作中心,0 库存量,0 预计生产,0 本次下单 FROM WLXQ Z LEFT JOIN ITEM D ON D.ITEM_CODE=Z.ID LEFT JOIN ITEM_PLANT ON D.ITEM_BUSINESS_ID=ITEM_PLANT.ITEM_ID LEFT JOIN ITEM_WAREHOUSE E ON E.ITEM_ID=ITEM_PLANT.ITEM_ID AND E.WAREHOUSE_ID=ITEM_PLANT.INBOUND_WAREHOUSE_ID LEFT JOIN WORK_CENTER H ON H.WORK_CENTER_ID=ITEM_PLANT.WORK_CENTER_ID LEFT JOIN INVMB ON MB001=D.ITEM_CODE LEFT JOIN V_WLPH ON TD004=MB001 WHERE Z.LB='需求' AND (ITEM_PLANT.ITEM_PROPERTY='M' OR ITEM_PLANT.ITEM_PROPERTY='S') AND (D.ITEM_CODE LIKE '%" + textBox1.Text + "%' OR D.ITEM_NAME LIKE '%" + textBox1.Text + "%' OR D.ITEM_SPECIFICATION LIKE '%" + textBox1.Text + "%') AND XQRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker1.Value.ToShortDateString() + "'),112) GROUP BY D.ITEM_BUSINESS_ID,D.ITEM_CODE,D.ITEM_NAME,D.ITEM_SPECIFICATION,ISNULL(H.WORK_CENTER_NAME,''),MB036";
            //textBox2.Text = strSql;
            try
            {
                DataTable dt;
                this.gridControl1.DataSource = "";
                this.gridControl1.Refresh();
                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                //dt.Rows.Add();
                this.gridControl1.DataSource = dt;
                gridView1.OptionsView.EnableAppearanceEvenRow = true;
                gridView1.OptionsView.EnableAppearanceOddRow = false;
            }
            catch
            {

            }
            progressBar1.Visible = true;
            progressBar1.Maximum = gridView1.RowCount;
            progressBar1.Value = 0;//设置当前值
            progressBar1.Step = 1;//设置没次增长多少
            for (int i=0;i<=gridView1.RowCount-1;i++)
            {
                progressBar1.Value += progressBar1.Step;
                strSql = "SELECT KCSL,YJSC FROM V_WLPH WHERE TD004='" + this.gridView1.GetRowCellValue(i, "品号").ToString() + "'";
                //MessageBox.Show(strSql,"提示");
                try
                {
                    sdr1 = db.GetDataReader1(strSql);

                    while (sdr1.Read())
                    {                
                        string kcsl = sdr1["KCSL"].ToString();
                        this.gridView1.SetRowCellValue(i, "库存量", Convert.ToDouble(kcsl));
                        string yjsc = sdr1["YJSC"].ToString();
                        this.gridView1.SetRowCellValue(i, "预计生产", Convert.ToDouble(yjsc));
                        Double bcxd = Convert.ToDouble(this.gridView1.GetRowCellValue(i, "需求数量").ToString()) - Convert.ToDouble(this.gridView1.GetRowCellValue(i, "库存量").ToString()) - Convert.ToDouble(this.gridView1.GetRowCellValue(i, "预计生产").ToString());                    
                        if(bcxd<0)
                        {
                            this.gridView1.SetRowCellValue(i, "本次下单", 0);
                        }
                        else
                        {
                            this.gridView1.SetRowCellValue(i, "本次下单", bcxd);
                        }
                        

                        //this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总需求"].Index].Value = Convert.ToDouble(sdr1["总需求"]).ToString();
                        //wlph = Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总库存"].Index].Value) + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已购未回量"].Index].Value) + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总生产量"].Index].Value) - Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总需求"].Index].Value);
                        //this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["物料平衡状况"].Index].Value = wlph;
                    }
                }
                catch 
                {
                   
                }
                finally
                {
                    sdr1.Close();
                }
            }
            progressBar1.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {            
            String strSql = "";
            strSql = "DELETE FROM MO_TEMP WHERE DLH='" + PropertyClass.OperatorCode + "'";
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                }
                else
                {
                }
            }
            catch
            {
            }

            int[] rows = this.gridView1.GetSelectedRows();
            foreach (int row in rows)
            {
                String mo_id = "00000000-0000-0000-0000-000000000000";
                String mo_d_id = "00000000-0000-0000-0000-000000000000";
                String item_id = this.gridView1.GetRowCellValue(row, "ITEM_ID").ToString();
                String qty = this.gridView1.GetRowCellValue(row, "本次下单").ToString();
                String bz = "";
                String xqrq = this.gridView1.GetRowCellValue(row, "预计开工").ToString();
                if (Convert.ToDouble(this.gridView1.GetRowCellValue(row, "本次下单").ToString()) > 0.00)
                {
                    strSql = "INSERT MO_TEMP SELECT '" + PropertyClass.OperatorCode + "','" + mo_id + "','" + mo_d_id + "','" + item_id + "'," + qty + ",'" + bz + "','','" + xqrq + "'";
                    //textBox1.Text = strSql;
                    //return;
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                        }
                        else
                        {

                        }
                    }
                    catch
                    {

                    }
                }
            }

            strSql = "EXEC [SP_MO_TEMP] '" + PropertyClass.OperatorCode + "','5110','" + DateTime.Now.ToString() + "'";
            //textBox3.Text = strSql;
            try
            {
                if (db.ExecDataBySql1(strSql) > 0)
                {
                   
                }
                else
                {

                }
            }
            catch
            {

            }

            MessageBox.Show("工单生成成功", "系统提示");
            button1.PerformClick();


        }

        private void JHRWGL_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now.AddDays(-7).Date;
       /*     repositoryItemComboBox1.Items.Add("装配一线");
            repositoryItemComboBox1.Items.Add("装配二线");
            repositoryItemComboBox1.Items.Add("装配三线");
            repositoryItemComboBox1.Items.Add("子件组");
            repositoryItemComboBox1.Items.Add("组件组");
            repositoryItemComboBox1.Items.Add("冲压组");
            repositoryItemComboBox1.Items.Add("钣金车间");
            repositoryItemComboBox1.Items.Add("准备组");
            repositoryItemComboBox1.Items.Add("蒸烤箱线");
            repositoryItemComboBox1.Items.Add("总装车间");*/

            string strSql = "SELECT WORK_CENTER_NAME FROM WORK_CENTER";
            try
            {
                sdr1 = db.GetDataReader1(strSql);
                while (sdr1.Read())
                {
                    repositoryItemComboBox1.Items.Add(sdr1["WORK_CENTER_NAME"].ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            finally
            {
                sdr1.Close();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            button1.PerformClick();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            String strSql = "";
            strSql = "SELECT CONVERT(CHAR(8),A.DOC_DATE,112) 下单日期1,A.DOC_NO 工单单号,B.ITEM_CODE 产品品号,B.ITEM_NAME 产品品名1,B.ITEM_SPECIFICATION 产品规格,CAST(PLAN_QTY AS REAL) 预计产量,A.REMARK 备注,ISNULL(H.WORK_CENTER_NAME,'') 工作中心1,PLAN_START_DATE 预计开工日,PLAN_COMPLETE_DATE 预计完工日,A.MO_ID FROM MO A  LEFT JOIN WORK_CENTER H ON H.WORK_CENTER_ID=A.SOURCE_ID_ROid LEFT JOIN ITEM B ON A.ITEM_ID=B.ITEM_BUSINESS_ID WHERE ISNULL(H.WORK_CENTER_NAME,'') LIKE '%" + textBox2.Text + "%' AND A.DOC_DATE>='" + dateTimePicker2.Value.ToShortDateString() + "' AND A.DOC_DATE>='" + dateTimePicker3.Value.ToShortDateString() + "'";
            //textBox1.Text = strSql;
            try
            {
                DataTable dt;
                this.gridControl2.DataSource = "";
                this.gridControl2.Refresh();

                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                //dt.Rows.Add();
                this.gridControl2.DataSource = dt;
                gridView2.OptionsView.EnableAppearanceEvenRow = true;
                gridView2.OptionsView.EnableAppearanceOddRow = false;
            }
            catch
            {

            }
        }

        private void 向下填充ToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
            String hb = gridView2.FocusedColumn.Name.ToString();
            String gzzx = this.gridView2.GetFocusedRowCellValue(hb).ToString();
            int[] rows = this.gridView2.GetSelectedRows();
            foreach (int row in rows)
            {
                gridView2.SetRowCellValue(row, hb, gzzx);

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string strSql = "";
            int[] rows = this.gridView2.GetSelectedRows();
            foreach (int row in rows)
            {
                String mo_id = this.gridView2.GetRowCellValue(row, "MO_ID").ToString();
                String gzzx = this.gridView2.GetRowCellValue(row, "工作中心1").ToString();
                String yjkg = this.gridView2.GetRowCellValue(row, "预计开工日").ToString();
                String yjwg = this.gridView2.GetRowCellValue(row, "预计完工日").ToString();
                strSql = "UPDATE MO SET SOURCE_ID_ROid=(SELECT WORK_CENTER_ID FROM WORK_CENTER WHERE WORK_CENTER_NAME='" + gzzx + "'),PLAN_START_DATE='" + yjkg + "',PLAN_COMPLETE_DATE='" + yjwg + "' WHERE MO_ID='" + mo_id + "'";
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                       
;                    }
                    else
                    {

                    }
                }
                catch
                {

                }
            }

            MessageBox.Show("修改成功！", "系统提示");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            String strSql = "";
            strSql = "SELECT CONVERT(CHAR(8),A.DOC_DATE,112) 下单日期1,A.DOC_NO 工单单号,B.ITEM_CODE 产品品号,B.ITEM_NAME 产品品名1,B.ITEM_SPECIFICATION 产品规格,CAST(PLAN_QTY AS REAL) 预计产量,A.REMARK 备注,ISNULL(H.WORK_CENTER_NAME,'') 工作中心1,PLAN_START_DATE 预计开工日,PLAN_COMPLETE_DATE 预计完工日,A.MO_ID FROM MO A  LEFT JOIN WORK_CENTER H ON H.WORK_CENTER_ID=A.SOURCE_ID_ROid LEFT JOIN ITEM B ON A.ITEM_ID=B.ITEM_BUSINESS_ID WHERE A.DOC_NO LIKE '5110%' AND NOT EXISTS(SELECT * FROM MOCTY WHERE RTRIM(TY001)+'-'+RTRIM(TY002)=RTRIM(A.DOC_NO)) AND A.[STATUS]<>'Y' AND A.[STATUS]<>'y'";
            //textBox3.Text = strSql;
            try
            {
                DataTable dt;
                this.gridControl1.DataSource = "";
                this.gridControl1.Refresh();
                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                //dt.Rows.Add();
                this.gridControl3.DataSource = dt;
                gridView1.OptionsView.EnableAppearanceEvenRow = true;
                gridView1.OptionsView.EnableAppearanceOddRow = false;
            }
            catch
            {

            }
        }
    }
}
